/******************************************************************************/
/*                                                                            */
/*  Copyright (c) 2009: ngi-central.org                                       */
/*  All rights reserved.                                                      */
/*                                                                            */
/******************************************************************************/


#include <boost/test/unit_test.hpp>

//#include "..\..\include\sgl\box.h"
//#include "..\..\include\sgl\hyper_sphere.h"
//#include "..\..\include\sgl\hyper_ellipsoid.h"
#include "..\..\include\sgl\polygon.h"
#include "..\..\include\sgl\moments.h"
#include "..\..\include\sgl\normalized_moments.h"

BOOST_AUTO_TEST_SUITE( moments2d )

BOOST_AUTO_TEST_CASE( moments__box2d )
{
	sgl::box<2> a;

	sgl::unnormalized_moments<> m = sgl::moments(a);

	BOOST_CHECK( m.m00() == 0. );
	BOOST_CHECK( m.m10() == 0. );
	BOOST_CHECK( m.m01() == 0. );
	BOOST_CHECK( m.m20() == 0. );
	BOOST_CHECK( m.m11() == 0. );
	BOOST_CHECK( m.m02() == 0. );
	BOOST_CHECK( m.m30() == 0. );
	BOOST_CHECK( m.m21() == 0. );
	BOOST_CHECK( m.m12() == 0. );
	BOOST_CHECK( m.m03() == 0. );

	sgl::normalized_moments<> nm;
	BOOST_CHECK_THROW( nm = sgl::normalized_moments<>(m), sgl::precondition_violated );

	BOOST_CHECK( nm.nm10() == 0. );
	BOOST_CHECK( nm.nm01() == 0. );
	BOOST_CHECK( nm.nm20() == 0. );
	BOOST_CHECK( nm.nm11() == 0. );
	BOOST_CHECK( nm.nm02() == 0. );
	BOOST_CHECK( nm.nm30() == 0. );
	BOOST_CHECK( nm.nm21() == 0. );
	BOOST_CHECK( nm.nm12() == 0. );
	BOOST_CHECK( nm.nm03() == 0. );

	sgl::box<2> b(
		sgl::point<2>(1, 2),
		sgl::vector<2>(3, 4));

	sgl::unnormalized_moments<> m1 = sgl::moments(b);

	BOOST_CHECK( m1.m00() == 12. );
	BOOST_CHECK( m1.m10() == 30. );
	BOOST_CHECK( m1.m01() == 48. );
	BOOST_CHECK( m1.m20() == 84. );
	BOOST_CHECK( m1.m11() == 120. );
	BOOST_CHECK( m1.m02() == 208. );
	BOOST_CHECK( m1.m30() == 255. );
	BOOST_CHECK( m1.m21() == 560. );
	BOOST_CHECK( m1.m12() == 866.66666666666663 );
	BOOST_CHECK( m1.m03() == 960. );

	sgl::normalized_moments<> nm1(m1);

	BOOST_CHECK( nm1.nm10() == 2.5 );
	BOOST_CHECK( nm1.nm01() == 4. );
	BOOST_CHECK( nm1.nm20() == 7. );
	BOOST_CHECK( nm1.nm11() == 10. );
	BOOST_CHECK( nm1.nm02() == 17.333333333333332 );
	BOOST_CHECK( nm1.nm30() == 21.25 );
	BOOST_CHECK( nm1.nm21() == 46.666666666666664 );
	BOOST_CHECK( nm1.nm12() == 72.222222222222214 );
	BOOST_CHECK( nm1.nm03() == 80. );
}				   

BOOST_AUTO_TEST_CASE( moments__polygon2d )
{
	sgl::polygon<2> a(
		sgl::quadrilateral<2>(
			sgl::point<2>( 2, 0),
			sgl::point<2>(10, 4),
			sgl::point<2>( 8, 8),
			sgl::point<2>( 0, 4)));

	sgl::unnormalized_moments<> m = sgl::moments(a);

	BOOST_CHECK( m.m00() == 40. );
	BOOST_CHECK( m.m10() == 200. );
	BOOST_CHECK( m.m01() == 160. );
	BOOST_CHECK( m.m20() == 1226.6666666666667 );
	BOOST_CHECK( m.m11() == 880. );
	BOOST_CHECK( m.m02() == 746.66666666666663 );
	BOOST_CHECK( m.m30() == 8400. );
	BOOST_CHECK( m.m21() == 9511.1111111111113 );
	BOOST_CHECK( m.m12() == 7288.8888888888887 );
	BOOST_CHECK( m.m03() == 3840. );

	sgl::normalized_moments<> nm(m);

	BOOST_CHECK( nm.nm10() == 5. );
	BOOST_CHECK( nm.nm01() == 4. );
	BOOST_CHECK( nm.nm20() == 30.666666666666668 );
	BOOST_CHECK( nm.nm11() == 22. );
	BOOST_CHECK( nm.nm02() == 18.666666666666664 );
	BOOST_CHECK( nm.nm30() == 210. );
	BOOST_CHECK( nm.nm21() == 237.77777777777777 );
	BOOST_CHECK( nm.nm12() == 182.22222222222223 );
	BOOST_CHECK( nm.nm03() == 96. );

/*

	sgl::box<2> b(
		sgl::point<2>(1, 2),
		sgl::vector<2>(3, 4));

	BOOST_CHECK( moments(b) == sgl::unnormalized_moments<>() );
*/
}

BOOST_AUTO_TEST_SUITE_END()

